package com.eddie.stack;

public class ArrayStackDemo {
    public static void main(String[] args) {
        ArrayStack arrayStack = new ArrayStack(5);
        arrayStack.push(2);
        arrayStack.push(5);
        arrayStack.push(6);
        System.out.println("原始");
        arrayStack.show();
        System.out.println("取出");
        System.out.println(arrayStack.pop());
        System.out.println("取出后");
        arrayStack.show();

    }
}

/**
 * 定义一个arrayStack表示栈结构
 */
class ArrayStack {
    /**
     * 栈的大小
     */
    private int maxSize;
    /**
     * 数组模拟栈
     */
    private int[] stack;
    /**
     * top表示栈顶
     */
    private int top = -1;

    public ArrayStack(int maxSize) {
        this.maxSize = maxSize;
        stack = new int[maxSize];
    }

    public boolean isFull() {
        return top == maxSize - 1;
    }

    public boolean isEmpty() {
        return top == -1;
    }

    public void push(int num) {
        if (!isFull()) {
            top++;
            stack[top] = num;
        } else {
            System.out.println("栈已满");
        }
    }

    public int pop() {
        if (isEmpty()) {
            System.out.println("栈为空");
        } else {
            return stack[top--];
        }
        return -1;
    }

    /**
     * 遍历栈
     */
    public void show() {
        if (!isEmpty()) {
            for (int i = top; i >= 0; i--) {
                System.out.println(stack[i]);
            }
        }
    }


}
